package com.axend.aerosense.home.fragment;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModelProvider;

import com.axend.aerosense.base.fragment.MvvmBaseFragment;
import com.axend.aerosense.base.impl.ILoadDataResult;
import com.axend.aerosense.base.impl.IOperationResult;
import com.axend.aerosense.base.livedatabus.LiveDatabus;
import com.axend.aerosense.base.viewmodel.BaseViewModelFactroy;
import com.axend.aerosense.common.base.ITitle;
import com.axend.aerosense.common.bean.EarlyWarningBean;
import com.axend.aerosense.common.bean.LogType;
import com.axend.aerosense.common.config.Gobal;
import com.axend.aerosense.home.entity.ReportInfoBean;
import com.axend.aerosense.home.viewmodel.HomeReportDetailsFragmentViewModel;
import com.axend.aerosense.home.R;
import com.axend.aerosense.home.databinding.HomeFragmentReportDetailsBinding;

import static com.axend.aerosense.common.config.Gobal.EVENT_KEY.REPORT_EVENT_CHANGE;

public class ReportLogDetailsFragment extends MvvmBaseFragment<HomeFragmentReportDetailsBinding, HomeReportDetailsFragmentViewModel>
        implements ILoadDataResult<ReportInfoBean> {
    public final static String EarlyWarn = "EarlyWarn";
    private EarlyWarningBean earlyWarningBean;

    public static ReportLogDetailsFragment getInstance(EarlyWarningBean earlyWarningBean) {
        ReportLogDetailsFragment fragment = new ReportLogDetailsFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable(EarlyWarn, earlyWarningBean);
        fragment.setArguments(bundle);   //设置参数
        return fragment;
    }

    @Override
    protected void initParameters() {
        super.initParameters();
        Bundle bundle = getArguments();
        if (null != bundle) {
            this.earlyWarningBean = (EarlyWarningBean) bundle.getSerializable(EarlyWarn);
        }
    }

    @Override
    public int getLayoutId() {
        return R.layout.home_fragment_report_details;
    }

    @Override
    public int getBindingVariable() {
        return 0;
    }


    @Override
    protected HomeReportDetailsFragmentViewModel getViewModel() {
        BaseViewModelFactroy<ReportInfoBean> myViewModelFactroy = new BaseViewModelFactroy<>(this, this);
        HomeReportDetailsFragmentViewModel tmp = new ViewModelProvider(this, myViewModelFactroy).get(HomeReportDetailsFragmentViewModel.class);
        tmp.setLogUuid(earlyWarningBean.getEarlyWarningUuid());
        return tmp;
    }

    @Override
    protected void onRetryBtnClick() {
        showLoading();
        viewModel.onLoad();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        init();
    }

    private void init() {
        viewDataBinding.homeButReportDel.setOnClickListener(v -> {
            viewModel.onDeleteLog(getActivity(), new IOperationResult() {
                @Override
                public void onSuccess() {
                    LiveDatabus.getInstance().with(REPORT_EVENT_CHANGE, Integer.class).postValue(1);
                    LiveDatabus.getInstance().with(Gobal.EVENT_KEY.REFESH_HOME, Integer.class).postValue(1);
                    LiveDatabus.getInstance().with(Gobal.EVENT_KEY.REFESH_ROOM, Integer.class).postValue(1);
                    getActivity().finish();
                }

                @Override
                public void onFail() {

                }
            });
        });

        setLoadSir(viewDataBinding.homeContentView);
        showLoading();
        viewModel.onLoad();
    }

    @Override
    public void loadDataSuccess(ReportInfoBean bean) {
        String title = bean.getLogInfo().getEarlyWarningTypeName();
//        String tmp = getString(R.string.common_remind);
//        if(title.contains(tmp)){
//
//        }else{
//            title += tmp;
//        }
        ((ITitle)getActivity()).setTitle(title);

        viewDataBinding.homeTopImg.setBackgroundResource(R.mipmap.home_img_tx);
        viewDataBinding.setData(bean.getLogInfo());
        viewDataBinding.executePendingBindings();
//        setPromptInfo(bean.getLogInfo());
        if(bean.getLogInfo().getEarlyWarningLogState() == 0){
            // 读取未读预警,需要通知刷新相关界面
            LiveDatabus.getInstance().with(REPORT_EVENT_CHANGE, Integer.class).postValue(1);
            LiveDatabus.getInstance().with(Gobal.EVENT_KEY.REFESH_HOME, Integer.class).postValue(1);
            LiveDatabus.getInstance().with(Gobal.EVENT_KEY.REFESH_ROOM, Integer.class).postValue(1);
        }

    }

    private void setPromptInfo(EarlyWarningBean logInfo) {
        String content = "";
        String time = "10";
        if (logInfo.getEarlyWarningType() == LogType.DEVICE_OFFLINE.getType()) {
            //设备离线
            content = getString(R.string.home_tips_dev_offline);

        } else if (logInfo.getEarlyWarningType() == LogType.FALL_DOWN.getType()) {
            //摔倒
//            content = getString(R.string.);

        } else if (logInfo.getEarlyWarningType() == LogType.GO_OUT_NOT_BACK.getType()) {
            //外出未归
            content = getString(R.string.home_tips_never_come_back);
            content = String.format(content, time);

        } else if (logInfo.getEarlyWarningType() == LogType.INTRUDE.getType()) {
            //离家模式入侵报警
//            content = getString(R.string.);

        } else if (logInfo.getEarlyWarningType() == LogType.SLEEP_TIMEOUT.getType()) {
            //睡眠超时
            content = getString(R.string.home_tips_keep_sleeping_over_time);
            content = String.format(content, time);

        } else if (logInfo.getEarlyWarningType() == LogType.ContinuousSedentary.getType()) {
            //连续久坐
            content = getString(R.string.home_tips_continuous_sedentary);
            content = String.format(content, time);

        } else if (logInfo.getEarlyWarningType() == LogType.SittingLongTime.getType()) {
            //累积久坐
            content = getString(R.string.home_tips_accumulated_sedentary);
            content = String.format(content, time);

        } else if (logInfo.getEarlyWarningType() == LogType.ToiletDetention.getType()) {
            //卫生间滞留
            content = getString(R.string.home_tips_at_toilet_time);
            content = String.format(content, time);

        } else if (logInfo.getEarlyWarningType() == LogType.StandingStill.getType()) {
            //原地静止
            content = getString(R.string.home_tips_standing_still);
            content = String.format(content, time);

        } else if (logInfo.getEarlyWarningType() == LogType.HaveDinner.getType()) {
            //用餐
            content = getString(R.string.home_tips_not_eat_today);

        } else if (logInfo.getEarlyWarningType() == LogType.GetUpFrequently.getType()) {
            //频繁起夜
            content = getString(R.string.home_tips_times_of_getting_up_at_night);

        } else if (logInfo.getEarlyWarningType() == LogType.NightNotBack.getType()) {
            //起夜未归
            content = getString(R.string.home_tips_night_not_back);
            content = String.format(content, time);

        } else if (logInfo.getEarlyWarningType() == LogType.AbnormalWorkAndRest.getType()) {
            //作息异常
//            content = getString(R.string.);

        } else if (logInfo.getEarlyWarningType() == LogType.SleepQuality.getType()) {
            //睡眠质量
            content = getString(R.string.home_tips_frequent_at_night);

        } else if (logInfo.getEarlyWarningType() == LogType.ToiletUse.getType()) {
            //卫生间使用(频繁如厕)
            content = getString(R.string.home_tips_bathroom_often);

        } else if (logInfo.getEarlyWarningType() == LogType.FallTrend.getType()) {
            //跌倒趋势
//            content = getString(R.string.);

        }
//        viewDataBinding.homeTvPromptInfo.setText(content);

    }

    @Override
    public void loadDataFail(String decs) {

    }
}
